﻿using MediatR;
using Microsoft.EntityFrameworkCore.Storage;
using Process.API.Application.Command.Action.Report;
using Process.Domain;
using Process.ErrorCode;
using Process.Infrastructure.Implement;
using Process.Infrastructure.Interface;
using SqlSugar;

namespace Process.API.Application.Handler.Action.Report
{
    public class DelReportHandler :IRequestHandler<DelReportCommand ,APIResult<int>>
    {
        DatabaseContext db;
        IBaseRepository<ReportTable> repRep;

        public DelReportHandler(DatabaseContext db, IBaseRepository<ReportTable> repRep)
        {
            this.db = db;
            this.repRep = repRep;
        }

        public async Task<APIResult<int>> Handle(DelReportCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            // 批量逻辑删除
            if (request.ReportIds == null || request.ReportIds.Count == 0)
            {
                result.Code = APIEnum.Fail;
                result.Message = "未选择需要删除的记录";
                result.Data = 0;
                return await Task.FromResult(result);
            }

            var ml = await db.SqlSugar
                .Updateable<ReportTable>()
                .SetColumns(a => new ReportTable { IsDel = true })
                .Where(a => request.ReportIds.Contains(a.ReportId))
                .ExecuteCommandAsync();
            result.Code = APIEnum.Ok;
            result.Message = "报工记录删除成功";
            result.Data = ml;
            return await Task.FromResult(result);

        }
    }
}
